package com.example.demo.controller;

import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  定义提供给前端调用的api
 * */
@RestController
@RequestMapping("/user")
//@EnableAutoConfiguration // 不使用 swagger 时
@Api(value = "/user", description = "用户类型接口",tags={"用户类型接口"})
public class UserController {

    @Autowired
    //private UserDao userDao; // 简单版
    private IUserService userService;

    /**
     *  不使用 swagger 时候   @RequestMapping ， get @GetMapping()  post @PostMapping
     * */

    /**
     * 1、查询所有 URl：http://localhost:8080/user/findAll
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有", notes = "查询所有", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> findAll() {
        return userService.findAll();
    }

    /**
     * 2、查询一条记录 URl：http://localhost:8080/user/getUserById?id=39
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    @ApiOperation(value = "查询一条记录", notes = "查询一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public User getUserById(Integer id) {
        return userService.getUserById(id);
    }

    /**
     * 3、增加一条记录 URl：http://localhost:8080/user/insertUser?username=linhongcun&password=123
     * @param user
     */
    @PostMapping("/insertUser")
    @ApiOperation(value = "增加一条记录", notes = "增加一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public void insertUser(User user) {
        userService.insertUser(user);
    }

    /**
     * 4、更新一条记录 URl：http://localhost:8080/user/updateUser?id=43&username=linhongcun&password=123
     * @param user
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新一条记录", notes = "更新一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public void updateUser(User user) {
        userService.updateUser(user);
    }

    /**
     * 5、删除一条记录 URl：http://localhost:8080/user/deleteUser?id=43
     * @param id
     */
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除一条记录", notes = "删除一条记录", produces = MediaType.APPLICATION_JSON_VALUE)
    public void deleteUser(Integer id) {
        userService.deleteUser(id);
    }
}
